package br.ufc.parser;
import java.io.StringReader;
import java.util.HashSet;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;


public class Query {
	
	private String statement;
	private HashSet<Predicate> predicates;
	static final private CCJSqlParserManager parserManager = new CCJSqlParserManager();
	
	public Query(String query)  {
		super();
		this.statement = query;
		predicates = new HashSet<Predicate>();
		this.parse();
	}
	
	private void parse() {
		
//		System.out.println(this.getStatement());	
		try {
			Select select = (Select) parserManager.parse(new StringReader(statement));
			PlainSelect plainSelect = (PlainSelect) (select).getSelectBody();        
			
			String[] where = plainSelect.getWhere().toString().split("AND|OR");
			
			for (String predicate : where) {
				String[] parsedPredicate = predicate.trim().split(" "); 
				predicates.add(new Predicate(parsedPredicate[0].trim(), parsedPredicate[1].trim(), parsedPredicate[2].trim()));
			}
		} catch (JSQLParserException e) {
			e.printStackTrace();
		}			
	}

	public String getStatement() {
		return statement;
	}

	public void setStatement(String statement) {
		this.statement = statement;
	}

	public HashSet<Predicate> getPredicates() {
		return predicates;
	}

	public void setPredicates(HashSet<Predicate> predicates) {
		this.predicates = predicates;
	}

	
}
